<template>
    <el-aside :width="$store.state.isCollapse ? '180px' : '64px'">
        <el-menu
            default-active="2"
            class="el-menu-vertical-demo"
            :collapse="!$store.state.isCollapse"
            background-color="#545364"
            text-color="#fff"
            :collapse-transition="false"
        >
            <h3 style="text-align: center;">{{$store.state.isCollapse ? '后台管理' : '后台'}}</h3>
            <el-menu-item :index="item.path+''" v-for="(item,index) in noChildren()" :key="item.name" @click="clickMenu(item)">
                <!-- <el-icon><user /></el-icon> -->
                <component class="icons" :is="item.icon"></component>
                <template #title>{{item.label}}</template>
            </el-menu-item>
            <el-sub-menu :index="item.path+''" v-for="(item,index) in hasChildren()" :key="item.name">
                <template #title>
                    <component class="icons" :is="item.icon"></component>
                    <span>{{item.label}}</span>
                </template>
                <el-menu-item-group>
                    <el-menu-item :index="subItem.path+''" v-for="(subItem,subIndex) in item.children" :key="subIndex" @click="clickMenu(subItem)">
                        <component class="icons" :is="subItem.icon"></component>
                        {{ subItem.label }}
                    </el-menu-item>
                </el-menu-item-group>
            </el-sub-menu>
        </el-menu>
    </el-aside>
</template>



<script>
import {useRouter} from 'vue-router'
import { useStore } from 'vuex'
export default {
    setup(){
        const store = useStore()
        const router = useRouter()
        /* const list = [
            {
                path:'/user',
                name:'user',
                label:'用户管理',
                icon:'user',
                url:'UserManage/UserManage'
            },
            {
                label:'其他',
                icon:'location',
                path:'/other',
                children:[
                    {
                        path:'/page1',
                        name:'page1',
                        label:'页面1',
                        icon:'setting',
                        url:'Other/PageOne'
                    },
                    {
                        path:'/page2',
                        name:'page2',
                        label:'页面2',
                        icon:'setting',
                        url:'Other/PageTwo'
                    },
                ]
            },
        ] */
       
        const list = store.state.menu
        
        const noChildren =()=>{
            return list.filter(item=>!item.children)
        }
        const hasChildren =()=>{
            return list.filter(item=>item.children)
        }
        const clickMenu =(item)=>{
            router.push({name:item.name})
            //vuex 来管理
            store.commit("SELECT_MENU",item)
        }
        

        return {
            noChildren,
            hasChildren,
            clickMenu
        }
    }
}
</script>

<style lang="less" scoped>
.el-aside{
    height: 100%;
    background-color: #545364;
}
.icons{
    width: 18px;
    height: 18px;
    
}
.el-menu{
    border-right: none;
    h3{
        line-height: 48px;
        color:#fff;
    }
}
</style>